﻿namespace Extensions.Web.ViewModel.Design
{
    using System;
    using System.Web.UI;
    using System.Web.UI.Design;

    public class BindingExpressionEditor : ExpressionEditor
    {
        public override object EvaluateExpression(string expression, object parseTimeData, Type propertyType, IServiceProvider serviceProvider)
        {
            if (expression.Trim() != "")
            {
                ExpressionArgs args = new ExpressionArgs(expression);
                return ("{Binding:" + args.Expression + "}");
            }
            return expression;
        }

        public override ExpressionEditorSheet GetExpressionEditorSheet(string expression, IServiceProvider serviceProvider)
        {
            return new BindingExpressionEditorSheet(expression, this, serviceProvider);
        }

        private class BindingExpressionEditorSheet : ExpressionEditorSheet
        {
            private System.Web.UI.ViewModel.BindingType _bindingType;
            private string _propertyControlID;
            private string _propertyControlType;
            private string _propertyExpression;

            public BindingExpressionEditorSheet(string expression, BindingExpressionEditor expressionEditor, IServiceProvider serviceProvider) : base(serviceProvider)
            {
                if (expression.Trim() != "")
                {
                    ExpressionArgs args = new ExpressionArgs(expression);
                    this._propertyExpression = args.Expression;
                    this._propertyControlID = args.PropertyControlID;
                    this._propertyControlType = args.PropertyControlType;
                    this._bindingType = args.BindingType;
                }
                else
                {
                    this._propertyExpression = "";
                    this._propertyControlID = "";
                    this._propertyControlType = "";
                    this._bindingType = System.Web.UI.ViewModel.BindingType.Default;
                }
            }

            public override string GetExpression()
            {
                string str = "";
                if (this.PropertyControlID != "")
                {
                    str = str + "ID = " + this.PropertyControlID;
                }
                else if (this.PropertyControlType != "")
                {
                    str = str + "Type = " + this.PropertyControlType;
                }
                switch (this.BindingType)
                {
                    case System.Web.UI.ViewModel.BindingType.OneWay:
                        str = str + ", Mode = OneWay";
                        break;

                    case System.Web.UI.ViewModel.BindingType.TwoWay:
                        str = str + ", Mode = TwoWay";
                        break;
                }
                if (str != "")
                {
                    return (str + ", " + this.PropertyName);
                }
                return this.PropertyName;
            }

            public System.Web.UI.ViewModel.BindingType BindingType
            {
                get
                {
                    return this._bindingType;
                }
                set
                {
                    this._bindingType = value;
                }
            }

            public override bool IsValid
            {
                get
                {
                    try
                    {
                        ExpressionArgs args = new ExpressionArgs(this.GetExpression());
                        return (args.Expression != "");
                    }
                    catch
                    {
                        return false;
                    }
                }
            }

            public string PropertyControlID
            {
                get
                {
                    return this._propertyControlID;
                }
                set
                {
                    this._propertyControlID = value;
                    if (this._propertyControlID != "")
                    {
                        this._propertyControlType = "";
                    }
                }
            }

            public string PropertyControlType
            {
                get
                {
                    return this._propertyControlType;
                }
                set
                {
                    this._propertyControlType = value;
                    if (this._propertyControlType != "")
                    {
                        this._propertyControlID = "";
                    }
                }
            }

            public string PropertyName
            {
                get
                {
                    return this._propertyExpression;
                }
                set
                {
                    this._propertyExpression = value;
                }
            }
        }
    }
}

